mui.init();
(function($) {
	var myDate = new Date()
	var year = myDate.getFullYear()
	var month = myDate.getMonth() + 1;
	var data = myDate.getDate();

	document.getElementById("demo2").innerText = year + "-" + buLing(month) + "-" + buLing(data)
	document.getElementById("demo3").innerText = year + "-" + buLing(month) + "-" + buLing(data)
	var result = $('#result')[0];
	var btns = $('.btn');
	btns.each(function(i, btn) {
		btn.addEventListener('tap', function() {

			var optionsJson = this.getAttribute('data-options') || '{}';
			var options = JSON.parse(optionsJson);
			var id = this.getAttribute('id');
			var picker = new $.DtPicker(options);
			picker.show(function(rs) {
				if(Zepto("#demo2").text() < rs.text) {
					btns[i].innerText = rs.text;
					btns[i].style.color = "#333"
					picker.dispose();
				} else {
					mui.toast("开始时间不能小于结束时间")
					return
				}

			});
		}, false);
	});
})(mui);
var txt = JSON.parse(localStorage.getItem("text")) || {}
var mask = mui.createMask(ensconce)
/*$(".class").on("tap", "div", function() {
	if($(this).hasClass("xuanzhong")) {
		$(this).removeClass("xuanzhong")
	} else {
		$(this).addClass("xuanzhong")
	}
})*/
$(".modify").on("tap", function() {
	mask.close()
	ensconce()
})

function ensconce() {
	$(".confirm").css("display", "none")
}

var books = JSON.parse(localStorage.getItem("books")) || []
var html = ""
for(var i = 0; i < books.length; i++) {

	/*$(".book")[0].innerHTML*/
	html += '<div class="books" bookid=' + books[i].bookid + '><span><img src=' + books[i].booksrc + ' class="bookimg"/></span><div class="bookname">' + books[i].bookname + '</div><div class="remove"><img src="../../imges/Group 1073.png" /></div></div>'
}
$(".book").prepend(html)

window.onload = function() {
	$(".book").find("span").height($(".hei").height())
}
$(".numbook").text("共" + books.length + "本")
$(".remove").on("tap", function() {
	$(this).parents(".books").remove()
	var addbook = []
	var biao = $(".book").find(".books")
	for(var i = 0; i < biao.length; i++) {
		var obj = {}
		obj.bookid = biao.eq(i).attr("bookid")
		obj.booksrc = biao.eq(i).find(".bookimg")[0].src
		obj.bookname = biao.eq(i).find("div").text()
		addbook.push(obj)
	}
	localStorage.setItem('books', JSON.stringify(addbook))
	var books = JSON.parse(localStorage.getItem("books"))
	$(".numbook").text("共" + books.length + "本")
})

$(".release").on("tap", function() {
	if($(".mui-input-clear").val() == "") {
		mui.toast("请输入计划名称!")
		return
	}
	/*if(!$(".class").find("div").hasClass("xuanzhong")) {
		mui.toast("请选择发布班级!")
		return
	}*/
	if($(".books").eq(0).attr("bookid") == null) {
		mui.toast("请添加书本!")
		return
	}
	$(".confirm").css("display", "block")
	mask.show()
})

$(".confirms").on("tap", function() {

	var bookIds = ""
	for(var a = 0; a < $(".books").length; a++) {
		bookIds += $(".books").eq(a).attr("bookid") + ","
	}
	common.ajax("student/createPlan", {
		uid: common.uid,
		token: common.token,
		planName: $(".mui-input-clear").val(),
		bookIds: bookIds.slice(0, bookIds.length - 1),
		beginDate: $("#demo2").text(),
		endDate: $("#demo3").text()
	}, function(data) {
		if(data.code == 200) {
			mask.close()
			ensconce()
			$(".books").remove()
			localStorage.removeItem('books')
			localStorage.removeItem("text")
			$(".numbook").text("共0本")
			mui.toast("发布成功!")
			//			common.open('../home/index.html')
			var load = plus.webview.getWebviewById("TeacherProgramC.html");
			mui.fire(load, "release");
			var btnArray = ['返回上一页', '继续发布'];
			mui.confirm('您的计划/任务已经发布成功', '', btnArray, function(e) {
				if(e.index == 0) {
					mui.back()
				} else {
					location.reload()
				}
			})
		} else {
			mui.toast(data.message)
		}
	})
})
mui("body").on("tap", "#addend", function() {
	document.activeElement.blur();
	setTimeout(function() {
		var temp = {}
		var trus = []
		temp.txt = $(".mui-input-clear").val()
		temp.timeone = $("#demo2").text()
		temp.timetwo = $("#demo3").text()
		$.each($('.cls'), function(a, e) {
			if($(".cls").eq(a).hasClass("xuanzhong")) {
				trus.push(a)
			}
		})
		temp.trus = trus
		localStorage.setItem("text", JSON.stringify(temp))
		localStorage.setItem("state", 'true')
		mui.openWindow({
			url: "joinreading.html",
			id: "joinreading.html",
		})
	}, 300)
})
window.addEventListener('refresh', function(e) { //执行刷新
	location.reload();
});

if(txt.txt != undefined && txt.txt != '') {
	$(".mui-input-clear").val(txt.txt)
}
if(txt.timetwo != undefined && txt.timetwo != '') {
	$("#demo2").text(txt.timeone)
}
if(txt.timeone != undefined && txt.timeone != '') {
	$("#demo3").text(txt.timetwo)
}
var old_back = mui.back;
mui.back = function() {
	localStorage.removeItem('books')
	localStorage.removeItem("text")
	localStorage.removeItem("state")
	old_back();
}

$("input").on("focus", function() {
	$(".release").css("display", "none")
})
$("input").on("blur", function() {
	$(".release").css("display", "block")
})

function buLing(data) {
	if(data < 10) {
		data = "0" + data
	}
	return data
}